#include "widget.h"
#include "ui_widget.h"
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QMessageBox>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QSslSocket>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("客户端");
    manager = new QNetworkAccessManager(this);

    QNetworkAccessManager *manager1 = new QNetworkAccessManager(this);
    qDebug() << manager1->supportedSchemes();
    qDebug()<<QSslSocket::sslLibraryBuildVersionString();
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    // 1. 获取url
    QString url_str = ui->lineEdit->text();
    qDebug() << url_str;
    QUrl url(ui->lineEdit->text());
    // 2. 构建http请求
    QNetworkRequest req(url);
    // 3. 发送请求
    QNetworkReply* resp = manager->get(req);
    // 4.通过信号槽,来处理响应
    connect(resp,&QNetworkReply::finished,this,[=](){
        if(resp->error() == QNetworkReply::NoError)
        {
            // 获取响应
            QString html = resp->readAll();
            ui->plainTextEdit->setPlainText(html);
        }
        else
        {
            QMessageBox::critical(this,"错误",resp->errorString());
            ui->plainTextEdit->setPlainText(resp->errorString());
        }
        // 还需要释放 响应
        resp->deleteLater();
    });
}


